package io.github.wjehovah.yyn;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;


import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.TabPageIndicator;

import io.github.wjehovah.yyn.activity.AlertDialogActivity;
import io.github.wjehovah.yyn.activity.SubscriptActivity;
import io.github.wjehovah.yyn.data.Budejie;
import io.github.wjehovah.yyn.fragment.BaseBudejieFragment;
import io.github.wjehovah.yyn.fragment.BaseFragment;
import io.github.wjehovah.yyn.utils.SharedPrefUtils;

public class HomeGroup extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private RadioGroup bottomTabs;

    private FragmentManager manager;
    private FrameLayout contentContainer;
    @ViewInject(R.id.rl_main_top)
    private RelativeLayout mTitle;
    @ViewInject(R.id.top_toast_msg)
    private LinearLayout toastMsg;
    private int index;//标志底部 tab id

    @ViewInject(R.id.iv_top_title)
    private ImageView refreshImg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        View contentView = View.inflate(this, R.layout.activity_main, null);
        setContentView(contentView);
        ViewUtils.inject(this, contentView);
        //得到当前可见的ViewPager


        bottomTabs = (RadioGroup) findViewById(R.id.bottom_home_tabs);
        contentContainer = (FrameLayout) findViewById(R.id.content_container);
        initEvent();
        manager = getSupportFragmentManager();
        String positionInfo = SharedPrefUtils.getString(Budejie.POSITION, null);
        if (positionInfo != null) {
            index =Integer.valueOf( positionInfo.split("_")[0]);
        }
        switchTo(index);
        RadioButton button = (RadioButton) bottomTabs.getChildAt(0);
        button.setChecked(true);
        refreshImg.setOnClickListener(this);

        overridePendingTransition(R.anim.top_in, R.anim.bottom_out);


    }

    /**
     * 设置 RadioGroup 监听器
     */
    private void initEvent() {
        bottomTabs.setOnCheckedChangeListener(this);
    }


    private void switchTo(int position) {

        Log.e("yyn", position + "");
        BaseFragment fragment = new BaseFragment();
        Bundle args = new Bundle();
        args.putInt(Budejie.POSITION, position);
        fragment.setArguments(args);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.content_container, fragment);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.bottom_home_essence:
                index = 0;
                switchTo(index);
                break;
            case R.id.bottom_home_latest:
                index = 1;
                switchTo(index);
                break;
            case R.id.bottom_home_writeposts:
              /*  index = 2;
                switchTo(index);*/
                startActivity(new Intent(this, AlertDialogActivity.class));
                RadioButton button = (RadioButton) bottomTabs.getChildAt(2);
                button.setChecked(false);
                break;
            case R.id.bottom_home_news:
                index = 3;
                switchTo(index);
                break;
            case R.id.bottom_home_my:
                index = 4;
                switchTo(index);
                break;
        }
    }




    @Override
    public void onClick(View v) {
        //获得精华或者最新的adapter
        //实现 点击顶部 请求最新
        if (index == 0 || index == 1) {
            ObjectAnimator rotate = ObjectAnimator.ofFloat(mTitle.getChildAt(3), "rotation", 0f, 360f);
            rotate.start();
            ViewPager mViewPager = (ViewPager) ((LinearLayout) contentContainer.getChildAt(0)).getChildAt(1);
            int currentItem = mViewPager.getCurrentItem();
            BaseFragment.MyAdapter pagerAdapter = (BaseFragment.MyAdapter) mViewPager.getAdapter();
            BaseBudejieFragment budejieFragment =
                    (BaseBudejieFragment) pagerAdapter.instantiateItem(mViewPager, currentItem);
            budejieFragment.buttonPressed();

        }
    }


    public void subscript(View view) {
        startActivity(new Intent(this, SubscriptActivity.class));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
